package com.smyhvae.animatordemo.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.smyhvae.animatordemo.R;


/**
 * @author Lin·俊浩
 * @since 2017/9/28
 */

public class ListViewAdapter extends BaseAdapter{

    private String[] names;
    private Integer[] imageIds;
    private Context context;

    public ListViewAdapter(Context context, String[] names, Integer[] imageIds){
        this.names = names;
        this.imageIds = imageIds;
        this.context = context;
    }

    @Override
    public int getCount() {
        return names.length;
    }

    @Override
    public Object getItem(int position) {
        return names[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null){
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.list_view_item, null);
            holder.image = (ImageView) convertView.findViewById(R.id.image);
            holder.name = (TextView) convertView.findViewById(R.id.text_view);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.image.setBackgroundResource(imageIds[position]);
        holder.name.setText(names[position]);
        return convertView;
    }

    public final class ViewHolder{
        public ImageView image;
        public TextView name;
    }
}
